package com.forum.module.blog.controller.tag;

import cn.hutool.core.collection.CollUtil;
import com.forum.framework.common.pojo.DataResult;
import com.forum.framework.common.pojo.PageResult;
import com.forum.module.blog.controller.tag.vo.TagPageReqVO;
import com.forum.module.blog.controller.tag.vo.TagRespVO;
import com.forum.module.blog.controller.tag.vo.TagSavaReqVO;
import com.forum.module.blog.convert.tag.TagConvert;
import com.forum.module.blog.dal.dataobject.tag.TagDO;
import com.forum.module.blog.service.tag.TagService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import static com.forum.framework.common.pojo.DataResult.success;

/**
 * 功能:
 * 作者:ouyzh
 * 日期:2025/3/4 19:18
 */
@Tag(name = "博客管理 - 标签")
@RestController
@RequestMapping("blog/tag")
public class TagController {
    private final TagService tagService;

    public TagController(TagService tagService) {
        this.tagService = tagService;
    }

    @PostMapping("/create")
    @Operation(summary = "新增标签")
    public DataResult<Long> createTag(@RequestBody TagSavaReqVO reqVO) {
        Long id = tagService.createTag(reqVO);
        return success(id);
    }

    @PostMapping("/update")
    @Operation(summary = "更新标签")
    public DataResult<Boolean> updateTag(@RequestBody TagSavaReqVO reqVO) {
        tagService.updateTag(reqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除标签信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public DataResult<Boolean> deleteTag(@RequestParam("id") Long id) {
        tagService.deleteById(id);
        return success(true);
    }

    @PostMapping("/page")
    @Operation(summary = "分页查询标签列表")
    public DataResult<PageResult<TagRespVO>> page(@RequestBody @Valid TagPageReqVO pageReqVO) {
        // 获得标签分页列表
        PageResult<TagDO> pageResult = tagService.getTagPage(pageReqVO);
        if (CollUtil.isEmpty(pageResult.getList())) {
            return success(new PageResult<>(pageResult.getTotal()));
        }

        return success(new PageResult<>(TagConvert.INSTANCE.covertList(pageResult.getList()), pageResult.getTotal()));
    }
}
